﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// MemView.xaml 的交互逻辑
    /// </summary>
    public partial class MemView : UserControl
    {
        #region Backend Logics

        readonly CourseWrapper course;
        //WordWrapper word;

        #endregion

        #region Properties

        public bool IsFinish
        {
            get;
            private set;
        }

        #endregion

        public MemView()
        {
            InitializeComponent();
            //TestLoadCourse();
            //TestLoadWords();
        }

        public MemView(CourseWrapper cw)
            : this()
        {
            course = cw;
            // start recitation
            course.StartRecite();
            if (course.HasNextWordToRecite())
            {
                WordWrapper word = course.NextRecite;
                wordView1.SetWord(word);
                UpdateProgressBar(progressBar1);
                IsFinish = false;
            }
            else
            {
                MessageBox.Show("所有单词已经背完!");
                IsFinish = true;
            }
            
        }

        private void UpdateProgressBar(ProgressBar progressBar1)
        {
            if (course.TodayTotal == 0)
                progressBar1.Value = 100;
            else
                progressBar1.Value = course.TodayRecited * 100.0 / course.TodayTotal;//mark
        }

        private void ShowAfterButtonClicked()
        {
            //wordView1.ToggleExplanations(new string[] { "解释" }, true);
            wordView1.ToggleExplanations(wordView1.Contents.Keys.ToArray<string>(), true);
        }

        private void button_yes_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button4.Visibility = Visibility.Visible;
            button5.Visibility = Visibility.Visible;
            ShowAfterButtonClicked();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // user response 3
            course.UserResponse(wordView1.GetWord(), 3);
            // Go to next word
            TryNext();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // user response 1
            course.UserResponse(wordView1.GetWord(), 1);
            // Go to next word
            TryNext();
        }

        private void button_amb_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button_go.Visibility = Visibility.Visible;
            // user response 2
            course.UserResponse(wordView1.GetWord(), 2);
            ShowAfterButtonClicked();
        }

        private void button_no_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button_go.Visibility = Visibility.Visible;
            // user response 0
            course.UserResponse(wordView1.GetWord(), 0);
            ShowAfterButtonClicked();
        }

        private void button_go_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // Go to next word
            TryNext();
        }

        private void TryNext()
        {
            button_go.Visibility = Visibility.Hidden;
            UpdateProgressBar(progressBar1);

            if (course.HasNextWordToRecite())
            {
                wordView1.SetWord(course.NextRecite);
            }
            else
            {
                // 背完一个list
                //TODO
                MessageBox.Show("好样的！您已经完成一天的任务！");
                ((Grid)this.Parent).Children.Clear();
            }
        }

        private void wordView1_TagEventHandler(object sender, RoutedEventArgs e)
        {
            TagEventArgs args = (TagEventArgs)e;
            //DEBUG: wintokk
            WordWrapper word = wordView1.GetWord();
            //MessageBox.Show(word.Id + " " + word.Spell + " " + args.Tag);
            if (args.IsNew)
                word.AddTag(args.Tag);
            else
                word.RemoveTag(args.Tag);

            wordView1.Tags = TagRepository.ListWordTags(word);
        }

        private void button_no_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {

        }

    }
}
